package config

import com.zhaopin.script.library.inherent.build.Config

Config.build(binding) {

    version 'v1'

    coreVersion "v1"

    method_group "mbscore-default"

    cache {
        request {
            source getEnvSource("redis.source")
        }
    }

    converter {
        handler([
                convertInput().key("CV_WORK_YEAR").value(trans_dateFormat("yyyyMM")).condition(lt(trans_toIntValue("CV_WORK_YEAR"), 1)),
                convertInput().key("CV_MARITAL_STATUS").value("1").condition(eq(trans_toIntValue("CV_MARITAL_STATUS"), 3)),
                convertInput().key("ORG_ROOT_ORG_ID").value(getOrgRootOrgId("ORG_ROOT_ORG_ID")).condition(isNotBlank("S_SOU_FULL_INDEX")),
                convertInput().key("CV_WORKS_YEARS").value(getCVWorkYears("CV_WORK_YEAR")).condition(gt(trans_toIntValue("CV_WORK_YEAR"), 190001)),
                convertInput().key("CV_EDUCATION_LEVEL_NUM").value(trans_toIntValue("CV_EDUCATION_LEVEL")).condition(isNotBlank("CV_EDUCATION_LEVEL")),
                convertInput().key("CV_USER_DESIRED_SALARY_MAX1").value(getDesireSplit("salarymax", "1")).condition(isNotEmptyObject("CV_USER_PURPOSES")),
                convertInput().key("CV_USER_DESIRED_SALARY_MIN1").value(getDesireSplit("salarymin", "1")).condition(isNotEmptyObject("CV_USER_PURPOSES")),
                convertInput().key("CV_USER_DESIRED_SALARY_MAX2").value(getDesireSplit("salarymax", "2")).condition(isNotEmptyObject("CV_USER_PURPOSES")),
                convertInput().key("CV_USER_DESIRED_SALARY_MIN2").value(getDesireSplit("salarymin", "2")).condition(isNotEmptyObject("CV_USER_PURPOSES")),
                convertInput().key("CV_USER_DESIRED_SALARY_MAX3").value(getDesireSplit("salarymax", "3")).condition(isNotEmptyObject("CV_USER_PURPOSES")),
                convertInput().key("CV_USER_DESIRED_SALARY_MIN3").value(getDesireSplit("salarymin", "3")).condition(isNotEmptyObject("CV_USER_PURPOSES")),

                convertInput().key("CV_USER_DESIRED_CITY1").value(getDesireSplit("city", "1")).condition(isNotEmptyObject("CV_USER_PURPOSES")),
                convertInput().key("CV_USER_DESIRED_CITY2").value(getDesireSplit("city", "2")).condition(isNotEmptyObject("CV_USER_PURPOSES")),
                convertInput().key("CV_USER_DESIRED_CITY3").value(getDesireSplit("city", "3")).condition(isNotEmptyObject("CV_USER_PURPOSES")),

                convertInput().key("CV_USER_DESIRED_DISTRICT1").value(getDesireSplit("district", "1")).condition(isNotEmptyObject("CV_USER_PURPOSES")),
                convertInput().key("CV_USER_DESIRED_DISTRICT2").value(getDesireSplit("district", "2")).condition(isNotEmptyObject("CV_USER_PURPOSES")),
                convertInput().key("CV_USER_DESIRED_DISTRICT3").value(getDesireSplit("district", "3")).condition(isNotEmptyObject("CV_USER_PURPOSES")),

                convertInput().key("CV_USER_DESIRED_JOB_TYPE_1").value(getDesireSplit("jobtype", "1")).condition(isNotEmptyObject("CV_USER_PURPOSES")),
                convertInput().key("CV_USER_DESIRED_JOB_TYPE_2").value(getDesireSplit("jobtype", "2")).condition(isNotEmptyObject("CV_USER_PURPOSES")),
                convertInput().key("CV_USER_DESIRED_JOB_TYPE_3").value(getDesireSplit("jobtype", "3")).condition(isNotEmptyObject("CV_USER_PURPOSES")),

                convertInput().key("CV_USER_DESIRED_INDUSTRY").value(getDesireSplit("industry", "")).condition(isNotEmptyObject("CV_USER_PURPOSES")),

                convertInput().key("CV_DESIRED_SALARY_MAX_NUM").value(getMaxSalary("CV_USER_DESIRED_SALARY_MAX1", "CV_USER_DESIRED_SALARY_MAX2", "CV_USER_DESIRED_SALARY_MAX3")),
                convertInput().key("CV_DESIRED_SALARY_MIN_NUM").value(getMinSalary("CV_USER_DESIRED_SALARY_MIN1", "CV_USER_DESIRED_SALARY_MIN2", "CV_USER_DESIRED_SALARY_MIN3")),

                convertInput().key("S_SOU_FULL_INDEX_PIK").value(trans_analyzer("S_SOU_FULL_INDEX", "pik", true)).condition(isNotBlank("S_SOU_FULL_INDEX")),
                convertInput().key("S_SOU_FULL_INDEX_CIK").value(trans_analyzer("S_SOU_FULL_INDEX", "cik", true)).condition(isNotBlank("S_SOU_FULL_INDEX")),
                convertInput().key("S_SOU_FULL_INDEX_PIK_TERM_COUNT").value(transGetTermsCountForMatch("S_SOU_FULL_INDEX", "pik", true)).condition(isNotBlank("S_SOU_FULL_INDEX")),
                convertInput().key("S_SOU_FULL_INDEX_CIK_TERM_COUNT").value(transGetTermsCountForMatch("S_SOU_FULL_INDEX", "cik", true)).condition(isNotBlank("S_SOU_FULL_INDEX")),
                convertInput().key("S_SOU_FULL_INDEX_IK").value(trans_analyzer("S_SOU_FULL_INDEX", "ik", true)).condition(isNotBlank("S_SOU_FULL_INDEX")),
                convertInput().key("S_SOU_FULL_INDEX_IK_TERM_COUNT").value(transGetTermsCountForMatch("S_SOU_FULL_INDEX", "ik", true)).condition(isNotBlank("S_SOU_FULL_INDEX")),
                //命中率计算
                convertInput().key("MM_POSITION_NAME").value(mathCeil("S_SOU_FULL_INDEX_PIK_TERM_COUNT", 0.6)).condition(isNotBlank("S_SOU_FULL_INDEX_PIK_TERM_COUNT")),
                convertInput().key("MM_COMPANY_NAME").value(mathCeil("S_SOU_FULL_INDEX_CIK_TERM_COUNT", 0.8)).condition(isNotBlank("S_SOU_FULL_INDEX_CIK_TERM_COUNT")),
                convertInput().key("MM_POSITION_NAME_1").value(mathCeil("S_SOU_FULL_INDEX_PIK_TERM_COUNT", 1)).condition(isNotBlank("S_SOU_FULL_INDEX_PIK_TERM_COUNT")),

                convertInput().key("MM_COUTENT").value(getValue("S_SOU_FULL_INDEX_IK_TERM_COUNT")).condition(isNotBlank("S_SOU_FULL_INDEX")),
                convertInput().key("MM_COUTENT_PIK").value(getValue("S_SOU_FULL_INDEX_PIK_TERM_COUNT")).condition(isNotBlank("S_SOU_FULL_INDEX")),

                // term weight
                convertInput().key("queryTermWeightV3").value(replaceFunc("queryTermWeightV2")).condition(isNotBlank("queryTermWeightV2")),

                //queryJobtypeManual
                convertInput().key("QUERY_JOBTYPE_MANUAL_CRANK").value(getJsonValue("queryJobtypeManual", "queryCrank")).condition(isNotNull("queryJobtypeManual")),
                convertInput().key("QUERY_JOBTYPE_MANUAL_MATCHLEVEL").value(getJsonValue("queryJobtypeManual", "queryMatchLevel")).condition(isNotNull("queryJobtypeManual")),

                convertInput().key("QUERY_MATCH_LEVEL").value(concatQueryMatchLevel("QUERY_JOBTYPE_MANUAL_MATCHLEVEL")).condition(isNotNull("QUERY_JOBTYPE_MANUAL_MATCHLEVEL")),
                convertInput().key("QUERY_MATCH_LEVEL_COUNT").value(getTermCount("QUERY_MATCH_LEVEL")).condition(isNotNull("QUERY_MATCH_LEVEL")),

                convertInput().key("QUERY_MATCH_LEVEL_CONDITION").value(handleQueryMatch("QUERY_MATCH_LEVEL_COUNT", "QUERY_JOBTYPE_MANUAL_CRANK")).condition(isNotNull("QUERY_JOBTYPE_MANUAL_CRANK")),
                convertInput().key("QUERY_MATCH_LEVEL_USE").value(queryMatchlevel("QUERY_MATCH_LEVEL", "QUERY_MATCH_LEVEL_CONDITION")),
                convertInput().key("QUERY_MATCH_LEVELV1").value(concatQueryMatchLevelV1("QUERY_JOBTYPE_MANUAL_MATCHLEVEL")).condition(isNotBlank("QUERY_JOBTYPE_MANUAL_MATCHLEVEL")),
                convertInput().key("QUERY_MATCH_LEVEL_CONDITIONV1").value(getIntValue("QUERY_JOBTYPE_MANUAL_CRANK")).condition(isNotNull("QUERY_JOBTYPE_MANUAL_CRANK")),
                convertInput().key("QUERY_MATCH_LEVEL_USEV1").value(queryMatchlevelMiddle("QUERY_MATCH_LEVELV1", "QUERY_MATCH_LEVEL_CONDITIONV1")),

                convertInput().key("queryTerm").value(removeWeight("queryTermWeightV3")).condition(isNotBlank("queryTermWeightV3")),

                convertInput().key("QUERY_KEYWORD").value(formatQueryKeyword("queryKeyword4RecallEXP")).condition(isNotBlank("queryKeyword4RecallEXP")),

                convertInput().key("CV_DESIRED_JOBTYPE_THIRDLEVEL_1_SIMILAR_CHANGE")
                        .value(trans_concatString("CV_DESIRED_JOBTYPE_THIRDLEVEL_1", "CV_DESIRED_JOBTYPE_THIRDLEVEL_1_TO_SIMILAR"))
                        .condition(isNotNullOrEmpty("CV_DESIRED_JOBTYPE_THIRDLEVEL_1") || isNotBlank("CV_DESIRED_JOBTYPE_THIRDLEVEL_1_TO_SIMILAR")),
                convertInput().key("CV_DESIRED_JOBTYPE_THIRDLEVEL_2_SIMILAR_CHANGE")
                        .value(trans_concatString("CV_DESIRED_JOBTYPE_THIRDLEVEL_2", "CV_DESIRED_JOBTYPE_THIRDLEVEL_2_TO_SIMILAR"))
                        .condition(isNotNullOrEmpty("CV_DESIRED_JOBTYPE_THIRDLEVEL_2") || isNotBlank("CV_DESIRED_JOBTYPE_THIRDLEVEL_2_TO_SIMILAR")),
                convertInput().key("CV_DESIRED_JOBTYPE_THIRDLEVEL_3_SIMILAR_CHANGE")
                        .value(trans_concatString("CV_DESIRED_JOBTYPE_THIRDLEVEL_3", "CV_DESIRED_JOBTYPE_THIRDLEVEL_3_TO_SIMILAR"))
                        .condition(isNotNullOrEmpty("CV_DESIRED_JOBTYPE_THIRDLEVEL_3") || isNotBlank("CV_DESIRED_JOBTYPE_THIRDLEVEL_3_TO_SIMILAR")),
                convertInput().key("CV_DESIRE_ALL_SIM")
                        .value(concatIfNotNull("CV_DESIRED_JOBTYPE_THIRDLEVEL_1_SIMILAR_CHANGE","CV_DESIRED_JOBTYPE_THIRDLEVEL_2_SIMILAR_CHANGE","CV_DESIRED_JOBTYPE_THIRDLEVEL_3_SIMILAR_CHANGE"))
                        .condition(isNotNullOrEmpty("CV_DESIRED_JOBTYPE_THIRDLEVEL_1_SIMILAR_CHANGE") || isNotNullOrEmpty("CV_DESIRED_JOBTYPE_THIRDLEVEL_2_SIMILAR_CHANGE") || isNotNullOrEmpty("CV_DESIRED_JOBTYPE_THIRDLEVEL_3_SIMILAR_CHANGE") ),
                convertInput().key("CV_JOB_TITLE_1_PIK").value(trans_analyzer("CV_JOB_TITLE_1","pik",true)).condition(isNotBlank("CV_JOB_TITLE_1")),
                convertInput().key("CV_JOB_TITLE_1_PIK_TERM_COUNT").value(transGetTermsCountForMatch("CV_JOB_TITLE_1","pik",true)).condition(isNotBlank("CV_JOB_TITLE_1")),
                convertInput().key("queryJobtypeNoWeight").value(removeWeight("queryJobTypeV2")).condition(isNotBlank("queryJobTypeV2")),

                convertInput().key("CV_CHUNK_TITLE_KEY_FUNCTION_LAST_TMP").value(getJsonValue("CV_RESUME_INDEX", "CV_CHUNK_TITLE_KEY_FUNCTION_LAST")).condition(isNotNull("CV_RESUME_INDEX")),
                convertInput().key("CV_CHUNK_TITLE_KEY_FUNCTION_LAST_TMP2").value(getJsonValue("CV_RESUME_INDEX", "CV_CHUNK_TITLE_KEY_FUNCTION_LAST2")).condition(isNotNull("CV_RESUME_INDEX")),

                convertInput().key("CV_CHUNK_TITLE_BUSINESS_DIRCTION_LAST_TMP").value(getJsonValue("CV_RESUME_INDEX", "CV_CHUNK_TITLE_BUSINESS_DIRCTION_LAST")).condition(isNotNull("CV_RESUME_INDEX")),
                convertInput().key("CV_CHUNK_TITLE_KEY_FUNCTION_LAST").value(replaceFunc("CV_CHUNK_TITLE_KEY_FUNCTION_LAST_TMP")).condition(isNotBlank("CV_CHUNK_TITLE_KEY_FUNCTION_LAST_TMP")),
                convertInput().key("CV_CHUNK_TITLE_BUSINESS_DIRCTION_LAST").value(replaceFunc("CV_CHUNK_TITLE_BUSINESS_DIRCTION_LAST_TMP")).condition(isNotBlank("CV_CHUNK_TITLE_BUSINESS_DIRCTION_LAST_TMP")),
                convertInput().key("CDIN").value(replaceFunction("CV_DESIRED_INDUSTRY_NEW")).condition(isNotNull("CV_DESIRED_INDUSTRY_NEW")),
                convertInput().key("QuerySimilarSpace").value(replaceFunction("querySimilar")).condition(isNotNull("querySimilar")),
                convertInput().key("CV_CITY").value(replaceFunction("CV_DESIRED_CITY")).condition(isNotNull("CV_DESIRED_CITY")),
                convertInput().key("CCTKFL").value(concatCvChunk("CV_CHUNK_TITLE_KEY_FUNCTION_LAST_TMP")).condition(isNotNull("CV_CHUNK_TITLE_KEY_FUNCTION_LAST_TMP")),
                convertInput().key("CCTKFL2").value(concatCvChunk("CV_CHUNK_TITLE_KEY_FUNCTION_LAST_TMP2")).condition(isNotNull("CV_CHUNK_TITLE_KEY_FUNCTION_LAST_TMP2")),

                //新核心词表 同义词表
                convertInput().key("QUERY_KEYWORD_NEW").value(getNewQueryKeyWords("S_SOU_FULL_INDEX","queryKeywordForC")).condition(isNotBlank("S_SOU_FULL_INDEX")),
                convertInput().key("QUERY_SYNONYMSWORD").value(getNewQuerySynonymsWords("S_SOU_FULL_INDEX","querySynonymsForC")).condition(isNotBlank("S_SOU_FULL_INDEX")),

                convertInput().key("queryQ").value(getSimilarQry()).condition(isNotBlank("S_SOU_FULL_INDEX"))
        ])
    }

    fetcher([
            {
                request {
                    params {
                        query([
                                include("baseTopQ").toArray,
                                match().field("S_SOU_FULL_INDEX").mapper("SOU_RANKING_KEYWORD")
                        ])

                        fields "SOU_POSITION_ID," +
                                "score," +
                                "orgId:SOU_COMPANY_AUTOID," +
                                "jdCreateStaffId:JD_CREATE_STAFF_ID"


                        expand([
                                conditionQuery().query("{!collapse field=SOU_COMPANY_ID max=sum(mul(SOU_DUPLICATED,-10000),Tag_Double_TimeScore)}").condition(isNotBlank("S_SOU_EXPAND")).field("S_SOU_EXPAND")
                        ])
                        filter([
                                include("param-ps-base-0-FQ-fraud").toArray,
                                match().field("S_SOU_JOB_TYPE").mapper("SOU_JOB_TYPE,SOU_SALE_POSITION_TYPE"),
                                customQuery("+(SOU_SALE_TYPE: 101)")
                        ])

                        limit 5
                        weight 100

                        sorts([
                                sort("rand(10000)", "desc"),
                                sort("termfreq(SOU_RANKING_KEYWORD,'\${S_SOU_FULL_INDEX:-}')", "desc")
                        ])

                    }
                    condition conditionTop("needTop", "S_SOU_FULL_INDEX", "S_SOU_WORK_CITY")
                    source getEnvSource("solr.source_position_query")
                }

                config {
                    type "solr"
                    name "query-ps-mbscore-1"
                }
            },

            {
                request {
                    params {
                        query([
                                conditionQuery().query(getInput("queryQ")).needEscape(false).condition(isNotBlank("S_SOU_FULL_INDEX")).field("S_SOU_FULL_INDEX"),
                        ])

                        fields "SOU_POSITION_ID," +
                                "score," +
                                "orgId:SOU_COMPANY_AUTOID," +
                                "jdCreateStaffId:JD_CREATE_STAFF_ID," +
                                "[features_rerank]"

                        filter([
                                include("param-ps-base-update-FQ-fraud").toArray,
                                match().field("S_SOU_JOB_TYPE").mapper("SOU_JOB_TYPE,SOU_SALE_POSITION_TYPE"),
                                conditionQuery().query("-(JD_TITLE_SEG:实习)").condition(notTrue(isContainsFilter("S_SOU_FULL_INDEX","CV_WORKS_YEARS"))),
                                conditionQuery().query("-(JD_TITLE_SEG:应届)").condition(notTrue(isContainsFilter("S_SOU_FULL_INDEX","CV_WORKS_YEARS"))),
                                conditionQuery().query("-(JD_TITLE_SEG:校招)").condition(notTrue(isContainsFilter("S_SOU_FULL_INDEX","CV_WORKS_YEARS")))
                        ])

                        weight 0
                        limit 2000
                        cutoff 0
                        rerank {

                            result calculate().score(
                                    "rerankFeaturePositionNameQuery*100.0+"+
                                            "rerankFeatureJdprofeskilltag*20.0+" +
                                            "rerankFeatureCompanysizeCode*1.0+" +
                                            "rerankFeatureWorkExperienceFactor*5.0+" +
                                            "rerankFeatureIndustryCode*5.0+" +

                                            "rerankFeatureJdJobLevel3*5.0+" +
                                            "rerankFeatureSouEducationFactor*5.0+" +
                                            "rerankFeatureSalaryCode*10.0"

                            )

                                    .features([
                                            // query与jd_title相关性
                                            bean().name("rerankFeaturePositionNameQuery").query(rerankFeaturePositionNameQuery()),
                                            // query与 职位关键词相关
                                            bean().name("rerankFeatureJdprofeskilltag").query(rerankFeatureJdprofeskilltag()),
                                            // jd-三级职类与cv意向&当前职类
                                            bean().name("rerankFeatureJdJobLevel3").query(rerankFeatureJdJobLevel3()),
                                            // jd-薪资与意向薪资
                                            bean().name("rerankFeatureSalaryCode").query(rerankFeatureSalaryCode()),
                                            // jd-学历匹配
                                            bean().name("rerankFeatureSouEducationFactor").query(rerankFeatureSouEducationFactor()),
                                            // jd-工作年限匹配
                                            bean().name("rerankFeatureWorkExperienceFactor").query(rerankFeatureWorkExperienceFactor()),
                                            // company-行业与cv意向行业
                                            bean().name("rerankFeatureIndustryCode").query(rerankFeatureIndustryCode()),
                                            // company-公司规模
                                            bean().name("rerankFeatureCompanysizeCode").query(rerankFeatureCompanysizeCode()),
                                            bean().name("rerankFeatureRefresh").query(rerankFeatureRefresh())

                                    ])

                            use_rank_score true
                            rank_weight 0   //一排得分的权重
                            rerank_weight 1 //二排得分的权重
                            rerank_size 2000 //参与二排的个数
                            rerank_score 0 //参与二排的最低分
                            rerank_timeout 300
                            rerank_type "rerank_extend"
                            feature_name "rerankFeatureRefresh"

                        }
                        sorts([
                                sort("score", "desc"),
                                sort("SOU_SALE_TYPE", "desc")
                        ])

                    }

                    source getEnvSource("solr.source_position_query")
                }

                config {
                    type "solr"
                    name "query-ps-mbscore-3"
                }
            }

    ])

    merger {
        handler existsMerge(
                bean().fieldName("chatTotal").fieldType("long").mergeType("sum"),
                bean().fieldName("numFound").fieldType("long").mergeType("sum"),
                bean().fieldName("numTotal").fieldType("long").mergeType("sum"),
                bean().fieldName("debugs").fieldType("array").mergeType("append"),
                bean().fieldName("results").fieldType("array").mergeType("append").distinct("SOU_POSITION_ID"),
        )
    }

    filter([
            {
                request {
                    source getEnvSource("filter.rpc_cSearch_filter")
                }

                handler positionRepeatFilter("SOU_POSITION_ID")
                config {
                    type "custom"
                }
            }
    ])

    ranker([
            {
                exp "zml_ranker-position_search-position_mbscore-search_second_sort-v1.0"
                request {
                    source getEnvSource("ranker.rpc_zml_ranking")   //请求的数据源
                    params {
                        id "userId"
                        primaryField "SOU_POSITION_ID"
                        limit 1600
                        batchSize 80
                        onlyOriginal true
                        type "rpc"
                        sorted true
                        desc true
                    }
                }
                outLimitDrop true
                type "zml"
            },


            {
                exp "zml_ranker-position_search-position_mbscore-lambdamart_ctr_v1.5_reply_v1.1_retrain"
                request {
                    source getEnvSource("ranker.rpc_zml_ranking")   //请求的数据源
                    params {
                        id "userId"
                        primaryField "SOU_POSITION_ID"
                        onlyOriginal true
                        limit 600
                        type "rpc"
                        sorted true
                        desc true
                        list (["jdCreateStaffId"])
                    }
                }
                type "zml"
            }
    ])

    adjuster {
        handler {
            rules([ // index_of_jd_title=103 index_of_company_name_feature=102// b_content_company_name // b_content_job_title
                    zml_preProcess(), // 对应二排传到三排的预处理阶段
                    zml_rerank_filterWithCompanyJobType(), // 添加分层逻辑
                    zml_rerank_companyVarietyOrNot(),
                    zml_rerank_addTopAndRefreshScore(), // 添加置顶和刷新分
                    zml_rerank_finalSort(), // 对所有jd按照score从大到小的顺序排序
                    zml_rerank_finalSortUnit()// 防霸屏
            ])

            resultField([
                    bean().field("jd").mapper("SOU_POSITION_ID"),
                    bean().field("score").mapper("ctr_score")
                    // bean().field("relevenceScore").mapper("relevenceScore")
            ])
        }
    }

    caption {
        all include("positionReload")
    }
}
